package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.core.minidao.hibernate.dao.IGenericBaseCommonDao;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.dao.cms.FaqDao;
import net.dgg.dggcloud.service.jrcrm.entity.cms.Faq;
import net.dgg.dggcloud.service.jrcrm.vo.ValidateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/2.
 */
@Service
public class FaqService extends BaseService {

    @Autowired
    private FaqDao faqDao;

    @Resource(name = "miniDaoHiberCommonDao")
    protected IGenericBaseCommonDao repository;

    public void setFaqDao(FaqDao faqDao) {
        this.faqDao = faqDao;
    }

    public void setRepository(IGenericBaseCommonDao repository) {
        this.repository = repository;
    }

    /**
     *@Description: 根据 keyword 模糊查询（title、content）、status
     *@Author: litao
     *@Param:map
     *@Date:14:38 2017/11/6
     */
    public List<Map> listFaqByKeyWord(Map map) {

        return faqDao.listFaqByKeyWord(map);
    }

    /**
     *根据 applystage、applyproduct、applyrole、status、catgorycode、keyword （可选）查询
     *@Author: litao
     *@Param:map
     *@Date:14:38 2017/11/6
     */
    public List<Map> listFaqByCondition(Map map){
        return faqDao.listFaqByCondition(map);
    }
    /**
     *根据 applystage、applyproduct、applyrole、status、catgorycode、keyword （可选）查询总数
     *@Author: litao
     *@Param:map
     *@Date:14:38 2017/11/6
     */
    public Long countFaqByCondition(Map map){
        return faqDao.countFaqByCondition(map);
    }

    /**
     *默认查询前10条 根据helpful排序，如果有keyword则根据title查询
     *@Author: litao
     *@Param:map
     *@Date:14:38 2017/11/6
     */
    public List<Map> listFaqByHelpful(Map map){
       return faqDao.listFaqByHelpful(map);
    }

    public Integer countFaqByMap(Map map) {
        return faqDao.countFaqByMap(map);
    }

    public void deleteFaqByIdHiber(String[] ids) {
        for (String id : ids) {
            this.repository.<Faq>deleteEntityById(Faq.class, Long.parseLong(id));
        }
    }

    public void saveFaq(Faq faq, User curUser) {


            ValidateResult validateResult = validateFaq(faq);
            if(!validateResult.getSuccess())
            {
                throw new BizException(validateResult.getMsg(),0);
            }

//            faq.setId(keyWorker.nextId());

            faq.setStatus(2);   //待发布

            faq.setCreaterName(curUser.getName()+curUser.getLoginName());
            faq.setCreaterOrgId(curUser.getOrgId());
            faq.setCreaterId(curUser.getId());
            faq.setCreateTime(new Date());

            faq.setUpdaterName(curUser.getName()+ curUser.getLoginName());
            faq.setUpdaterOrgId(curUser.getOrgId());
            faq.setUpdaterId(curUser.getId());
            faq.setUpdateTime(new Date());


            this.repository.save(faq);
    }

    public void saveSysInfo(Faq faq, User curUser) {


            ValidateResult validateResult = validateSysInfo(faq);
            if(!validateResult.getSuccess())
            {
                throw new BizException(validateResult.getMsg(),0);
            }

            faq.setId(keyWorker.nextId());
            faq.setStatus(2);   //待发布

            faq.setCreaterName(curUser.getName()+curUser.getLoginName());
            faq.setCreaterOrgId(curUser.getOrgId());
            faq.setCreaterId(curUser.getId());
            faq.setCreateTime(new Date());

            faq.setUpdaterName(curUser.getName()+curUser.getLoginName());
            faq.setUpdaterOrgId(curUser.getOrgId());
            faq.setUpdaterId(curUser.getId());
            faq.setUpdateTime(new Date());


            this.repository.save(faq);
    }

    private ValidateResult validateFaq(Faq faq){
        ValidateResult validateResult = new ValidateResult();
        validateResult.setSuccess(true);
        if(StringUtils.isBlank(faq.getTitle())){
            validateResult.setSuccess(false);
            validateResult.setMsg("'标题'不能为空");
            return validateResult;
        }
        if(StringUtils.isBlank(faq.getContent())){
            validateResult.setSuccess(false);
            validateResult.setMsg("'正文'不能为空");
            return validateResult;
        }
        if(StringUtils.isBlank(faq.getCategoryCode())){
            validateResult.setSuccess(false);
            validateResult.setMsg("请选择'所属类别'");
            return validateResult;
        }
        if(StringUtils.isBlank(faq.getApplyArea())){
            validateResult.setSuccess(false);
            validateResult.setMsg("请选择'适用区域'");
            return validateResult;
        }
        if(StringUtils.isBlank(faq.getApplyStage())){
            validateResult.setSuccess(false);
            validateResult.setMsg("请选择'销售阶段'");
            return validateResult;
        }
        if(StringUtils.isBlank(faq.getApplyRole())){
            validateResult.setSuccess(false);
            validateResult.setMsg("请选择'适用角色'");
            return validateResult;
        }
        if(StringUtils.isBlank(faq.getApplyProduct())){
            validateResult.setSuccess(false);
            validateResult.setMsg("请选择'适用产品'");
            return validateResult;
        }
        return  validateResult;
    }

    private ValidateResult validateSysInfo(Faq faq){
        ValidateResult validateResult = new ValidateResult();
        validateResult.setSuccess(true);
        if(StringUtils.isBlank(faq.getTitle())){
            validateResult.setSuccess(false);
            validateResult.setMsg("'标题'不能为空");
            return validateResult;
        }
        if(StringUtils.isBlank(faq.getContent())){
            validateResult.setSuccess(false);
            validateResult.setMsg("'正文'不能为空");
            return validateResult;
        }

        return  validateResult;
    }

    public void batchUpdataStatusByIds(Long[] ids,Integer status){
        if(null == status){
            return;
        }
        if(null != ids && ids.length > 0){
            for(Long currentId : ids){
                Faq faq = repository.get(Faq.class,currentId);
                faq.setStatus(status);
                repository.saveOrUpdate(faq);
            }
        }
    }

    public void updateFaq(Faq faq,User currUser){
        ValidateResult validateResult = validateFaq(faq);
        if(!validateResult.getSuccess())
        {
            throw new BizException(validateResult.getMsg(),0);
        }
        Faq oldfaq = repository.get(Faq.class,faq.getId());
        if(null == faq){
            throw new BizException("无该数据或该数据已经彻底删除",0);
        }
        oldfaq.setTitle(faq.getTitle());
        oldfaq.setContent(faq.getContent());
        oldfaq.setCategoryCode(faq.getCategoryCode());
        oldfaq.setApplyStage(faq.getApplyStage());
        oldfaq.setApplyRole(faq.getApplyRole());
        oldfaq.setApplyProduct(faq.getApplyProduct());
        oldfaq.setApplyArea(faq.getApplyArea());

        oldfaq.setUpdateTime(new Date());
        oldfaq.setUpdaterId(currUser.getId());
        oldfaq.setUpdaterName(currUser.getName()+currUser.getLoginName());
        oldfaq.setUpdaterOrgId(currUser.getOrgId());

        repository.saveOrUpdate(oldfaq);
    }

    public void updateSysInfo(Faq faq,User currUser){
        ValidateResult validateResult = validateSysInfo(faq);
        if(!validateResult.getSuccess())
        {
            throw new BizException(validateResult.getMsg(),0);
        }
        Faq oldfaq = repository.get(Faq.class,faq.getId());
        if(null == faq){
            throw new BizException("无该数据或该数据已经彻底删除",0);
        }
        oldfaq.setTitle(faq.getTitle());
        oldfaq.setContent(faq.getContent());

        oldfaq.setUpdateTime(new Date());
        oldfaq.setUpdaterId(currUser.getId());
        oldfaq.setUpdaterName(currUser.getName()+currUser.getLoginName());
        oldfaq.setUpdaterOrgId(currUser.getOrgId());

        repository.saveOrUpdate(oldfaq);
    }

    public Faq getByIdHiber(Long id) {
        return this.repository.get(Faq.class, id);
    }

    public void addHelpful(Long id) {
        Faq faq = repository.get(Faq.class,id);
        faq.setHelpful(faq.getHelpful()+1);
    }
    public void addUnHelpful(Long id) {
        Faq faq = repository.get(Faq.class,id);
        faq.setUnhelpful(faq.getUnhelpful()+1);
    }

    public List<Map> sysInfoList(){
        return faqDao.querySysInfoListList();
    }
}
